<?php
session_start();
$ip=$_GET['ip'];
$mac=$_GET['mac'];
$port=$_GET['port'];
$cmd=$_GET['cmd'];
$change_type=$_GET['change_type'];
$debug=$_GET['debug'];
$current_user=$_SESSION['myusername'];
$access_profile=$_GET['subInternetSpeed'];


// Include class file that performs all the work.
include 'occam.php';
include('config.php');
// Create a new object to connect to the blade and perform caommands against
$coblade = &New occam($ip,"occam","razor","100/1");

// Open the session to the blade
$coblade->opentelnetsession();

// Issue commands against the blade, as many as you like.
// The second argument is a sleep timer that waits for command to finish.
// Commands like "show dsl summary" you may need to increase the timer to 5.
// the "show dsl summary" command takes a long time before it returns output.
#$coblade->sendcommand($cmd,"5");
//while (list($key,$val) = @each($cmd)) {

	switch ($cmd) {
		case "eqn_lookup":
			list($ig, ,$eqn1, $eqn2) = explode("-", $_GET['eqn']);
			if ($ig == "5400") {
			        $ig = "1";
			} else if ($ig == "5410") {
			        $ig = "2";
			} else if ($ig == "5420") {
			        $ig = "3";
			} else {
			// If the IG is unknown return NULL so that query fails
			        $ig = NULL;
			}

			// If the IG is not NULL, then proceed with query
			if ($ig != NULL || strlen($eqn2) > 2) {
			        if (strlen($eqn2) < 2) {
					// Example:  5400-0-1-1 has a CRV of 101, NOT 11
					$eqn2 = "0" . $eqn2;
				}
				$crv = $eqn1 . $eqn2;
				$coblade->eqn_lookup($ig,$crv,"1");
			} else {
			        echo "Invalid EQN";
			}
			break;
		case "occam_copper_add_customer":
			$dsl_profile = $_GET['subDslProfile'];
			$access_profile = $_GET['subInternetSpeed'];
			$video = $_GET['subVideoService'];
			$subName = $_GET['subName'];
			$subPhone = $_GET['subPhone'];
			$subCablePair = $_GET['subCablePair'];
			$coblade->occam_add_customer($port,"dsl",$subName,$subPhone,$subCablePair,$dsl_profile,$access_profile,$video,"1");
			break;
		case "occam_fiber_add_customer":
			$dsl_profile = NULL;
			$access_profile = $_GET['subInternetSpeed'];
			$video = $_GET['subVideoService'];
			$subName = $_GET['subName'];
			$subPhone = $_GET['subPhone'];
			$subOntIP = $_GET['subOntIP'];
			$coblade->occam_add_customer($port,"ethernet",$subName,$subPhone,$subOntIP,$dsl_profile,$access_profile,$video,"1");
			break;
		case "suspend_internet":
			$coblade->suspend_internet($port,"dsl","1");
			break;
		case "suspend_video":
			$coblade->suspend_video($port,"dsl","1");
			break;
		case "restore_internet":
			$coblade->restore_internet($port,"dsl","1");
			break;
		case "restore_video":
			$coblade->restore_video($port,"dsl","1");
			break;
		case "fiber_suspend_internet":
			$coblade->suspend_internet($port,"ethernet","1");
			break;
		case "fiber_suspend_video":
			$coblade->suspend_video($port,"ethernet","1");
			break;
		case "fiber_restore_internet":
			$coblade->restore_internet($port,"ethernet","1");
			break;
		case "fiber_restore_video":
			$coblade->restore_video($port,"ethernet","1");
			break;
		case "delete_dhcp_clients":
			$coblade->delete_dhcp_clients($port,$mac,"dsl","1");
			//$coblade->show_dhcp_clients($port,"dsl","1");
			break;
		case "show_copper_dhcp_clients":
			$coblade->show_dhcp_clients($port,"dsl","1");
			break;
		case "show_fiber_dhcp_clients":
			$coblade->show_dhcp_clients($port,"ethernet","1");
			break;
		case "standard_bronze":
			$coblade->speed_change($port,"ethernet",$cmd,"1");
			break;
		case "bronze_dsl":
			$coblade->speed_change($port,"dsl",$cmd,"1");
			break;
		case "silver_dsl":
			$coblade->speed_change($port,"dsl",$cmd,"1");
			break;
		case "gold_dsl":
			$coblade->speed_change($port,"dsl",$cmd,"1");
			break;
		case "platinum_dsl":
			$coblade->speed_change($port,"dsl",$cmd,"1");
			break;
		case "diamond_dsl":
			$coblade->speed_change($port,"dsl",$cmd,"1");
			break;
		case "bundle_dsl":
			$coblade->speed_change($port,"dsl",$cmd,"1");
			break;
		case "shut_no_shut":
			$coblade->shut_no_shut($port,"1");
			break;
		case "modem_status":
			$coblade->get_customer_info($port,"dsl","1");
		//TODO:  Find a cleaner way to do this!!
		//Get blade device_type via SNMP
		$snmp_device_type = preg_replace("/STRING: /", "", snmpget("$ip", "public", "sysDescr.0"));	
		//Figure out port mapping based on device_type
		//Display interface descriptions and find which port it is 1.3.6.1.2.1.2.2.1.2 (display port descriptions)
		//ALL deviations are based off of the 6252-02 blade
		if ( $snmp_device_type == "BLC 6252 02" OR $snmp_device_type == "BLC 6252" OR $snmp_device_type == "BLC 6252 03" AND !empty($port)) {
			switch ($port) {
				case 1:
					$snmp_map = 36;
					break;
				case 2:
					$snmp_map = 37;
					break;
				case 3:
					$snmp_map = 38;
					break;
				case 4:
					$snmp_map = 39;
					break;
				case 5:
					$snmp_map = 40;
					break;
				case 6:
					$snmp_map = 41;
					break;
				case 7:
					$snmp_map = 30;
					break;
				case 8:
					$snmp_map = 31;
					break;
				case 9:
					$snmp_map = 32;
					break;
				case 10:
					$snmp_map = 33;
					break;
				case 11:
					$snmp_map = 34;
					break;
				case 12:
					$snmp_map = 35;
					break;
				case 13:
					$snmp_map = 24;
					break;
				case 14:
					$snmp_map = 25;
					break;
				case 15:
					$snmp_map = 26;
					break;
				case 16:
					$snmp_map = 27;
					break;
				case 17:
					$snmp_map = 28;
					break;
				case 18:
					$snmp_map = 29;
					break;
				case 19:
					$snmp_map = 18;
					break;
				case 20:
					$snmp_map = 19;
					break;
				case 21:
					$snmp_map = 20;
					break;
				case 22:
					$snmp_map = 21;
					break;
				case 23:
					$snmp_map = 22;
					break;
				case 24:
					$snmp_map = 23;
					break;
				case 25:
					$snmp_map = 60;
					break;
				case 26:
					$snmp_map = 61;
					break;
				case 27:
					$snmp_map = 62;
					break;
				case 28:
					$snmp_map = 63;
					break;
				case 29:
					$snmp_map = 64;
					break;
				case 30:
					$snmp_map = 65;
					break;
				case 31:
					$snmp_map = 54;
					break;
				case 32:
					$snmp_map = 55;
					break;
				case 33:
					$snmp_map = 56;
					break;
				case 34:
					$snmp_map = 57;
					break;
				case 35:
					$snmp_map = 58;
					break;
				case 36:
					$snmp_map = 59;
					break;
				case 37:
					$snmp_map = 48;
					break;
				case 38:
					$snmp_map = 49;
					break;
				case 39:
					$snmp_map = 50;
					break;
				case 40:
					$snmp_map = 51;
					break;
				case 41:
					$snmp_map = 52;
					break;
				case 42:
					$snmp_map = 53;
					break;
				case 43:
					$snmp_map = 42;
					break;
				case 44:
					$snmp_map = 43;
					break;
				case 45:
					$snmp_map = 44;
					break;
				case 46:
					$snmp_map = 45;
					break;
				case 47:
					$snmp_map = 46;
					break;
				case 48:
					$snmp_map = 47;
					break;
				default:
					$snmp_map = "";
					break;
			}
		
			//Have to modify the snmp_map because of old blades that had more ethernet ports for EPS keys	
			if ($snmp_device_type == "BLC 6252") {
				$snmp_deviation = 1;
				$snmp_map = $snmp_map + $snmp_deviation;
				$snmp_map = strval($snmp_map);
				//echo $snmp_map;
			}
	
			if (!empty($snmp_map)){	
				$modem_operation_status = preg_replace("/INTEGER: /", "", snmpget("$ip", "public", "1.3.6.1.2.1.2.2.1.8.$snmp_map"));
				$modem_admin_status = preg_replace("/INTEGER: /", "", snmpget("$ip", "public", "1.3.6.1.2.1.2.2.1.7.$snmp_map"));
			}
		}
		
		if ($modem_operation_status == "down(2)") {
			$modem_operation_status = "down";
		}	
		else if ($modem_operation_status == "up(1)") {
			$modem_operation_status = "up";
		}
		else {
			$modem_operation_status = "ERROR:  Unknown status returned";
		}

		if ($modem_admin_status == "down(2)") {
			$modem_admin_status = "down";
		}
		else if ($modem_admin_status == "up(1)") {
			$modem_admin_status = "up";
		}
		else {
			$modem_admin_status = "ERROR:  Unknown status returned";
		}
		break;
		case "port_performance":
			$coblade->port_performance($port,"dsl","1");
			break;
		case "fiber_port_stats":
			$coblade->port_performance($port,"ethernet","1");
			break;
		case "ont_status":
			$coblade->get_customer_info($port,"ethernet","1");
			//TODO:  Find a cleaner way to do this!
			switch ($port) {
				case "1":
					$snmp_map = 25;
					break;
				case "2":
					$snmp_map = 26;
					break;
				case "3":
					$snmp_map = 23;
					break;
				case "4":
					$snmp_map = 24;
					break;
				case "5":
					$snmp_map = 21;
					break;
				case "6":
					$snmp_map = 22;
					break;
				case "7":
					$snmp_map = 20;
					break;
				case "8":
					$snmp_map = 19;
					break;
				case "9":
					$snmp_map = 18;
					break;
				case "10":
					$snmp_map = 17;
					break;
				case "11":
					$snmp_map = 16;
					break;
				case "12":
					$snmp_map = 15;
					break;
				case "13":
					$snmp_map = 14;
					break;
				case "14":
					$snmp_map = 13;
					break;
				case "15":
					$snmp_map = 12;
					break;
				case "16":
					$snmp_map = 11;
					break;
				case "17":
					$snmp_map = 10;
					break;
				case "18":
					$snmp_map = 9;
					break;
				case "19":
					$snmp_map = 8;
					break;
				case "20":
					$snmp_map = 7;
					break;
				case "21":
					$snmp_map = 6;
					break;
				case "22":
					$snmp_map = 5;
					break;
			}
			
			if (!empty($snmp_map)){	
				$modem_operation_status = preg_replace("/INTEGER: /", "", snmpget("$ip", "public", "1.3.6.1.2.1.2.2.1.8.$snmp_map"));
				$modem_admin_status = preg_replace("/INTEGER: /", "", snmpget("$ip", "public", "1.3.6.1.2.1.2.2.1.7.$snmp_map"));
			}
		
			if ($modem_operation_status == "down(2)") {
				$modem_operation_status = "down";
			}	
			else if ($modem_operation_status == "up(1)") {
				$modem_operation_status = "up";
			}
			else {
				$modem_operation_status = "ERROR:  Unknown status returned";
			}

			if ($modem_admin_status == "down(2)") {
				$modem_admin_status = "down";
			}
			else if ($modem_admin_status == "up(1)") {
				$modem_admin_status = "up";
			}
			else {
				$modem_admin_status = "ERROR:  Unknown status returned";
			}
			break;
	}//END of switch
//}//END of while loop on cmd's

//----  Start of function declarations ----

//----  End of function declarations ----

// Add changes to the transaction_log in the database for tracking purposes

if ( $cmd == "eqn_lookup" OR $cmd == "modem_status" OR $cmd == "modemStatus" OR $cmd == "port_performance" OR $cmd == "show_copper_dhcp_clients" OR $cmd == "show_fiber_dhcp_clients" OR $cmd == "ont_status" OR $cmd == "fiber_port_stats") {
//If command is just a query, do not log it
}
else {
	mysql_query("INSERT INTO transaction_log (trans_id, trans_type, trans_blade, trans_port, submitted_by) VALUES (NULL, '$cmd', '$ip', '$port', '$current_user')");
}

// Capture output and echo it to the screen
#echo $coblade->getoutput();
$output = $coblade->getoutput();


if ($cmd == "suspend_video" || $cmd == "suspend_internet" || $cmd == "restore_internet" || $cmd == "restore_video") {
	// This section parses the output for the access-profile and admin-status
	// from the "sh dsl service int atm $port" command
	preg_match("/0\/35(.*)/", $output, $internet_matches);
	preg_match("/0\/36(.*)/", $output, $video_matches);

	$internet_data = $internet_matches[1];
	$internet_data = preg_replace("/\s\s+/", ",", $internet_data);
	$video_data = $video_matches[1];
	$video_data = preg_replace("/\s\s+/", ",", $video_data);
	
	list( ,$internet_encapsulation, $internet_access_profile, $internet_admin_status) = explode(",", $internet_data);
	list( ,$video_encapsulation, $video_access_profile, $video_admin_status) = explode(",", $video_data);

	echo "Speed Profile: $internet_access_profile <br/>\n";

	if ($internet_admin_status == "up") {
		echo "Internet Admin Status: <font style=\"color: green; font-weight: bold\">$internet_admin_status</font>\n";
	}
	else {
		echo "Internet Admin Status: <font style=\"color: red; font-weight: bold\">$internet_admin_status</font>\n";
	}

	echo "<br/>\n";

	if ($video_admin_status == "up") {
		echo "Video Admin Status: <font style=\"color: green; font-weight: bold\">$video_admin_status</font>\n";
	}
	else {
		echo "Video Admin Status: <font style=\"color: red; font-weight: bold\">$video_admin_status</font>\n";
	}
}
else if ($cmd == "bronze_dsl" || $cmd == "silver_dsl" || $cmd == "gold_dsl" || $cmd == "platinum_dsl" || $cmd == "bundle_dsl" || $cmd == "standard_bronze" || $cmd == "diamond_dsl") {
	//TODO:  Clean up output display
	echo "Speed Profile changed to $cmd";
}
else if ($cmd == "delete_dhcp_clients" OR $cmd == "show_copper_dhcp_clients" OR $cmd == "show_fiber_dhcp_clients") {
	//DONE:  Show a form to delete specific dhcp clients "done on 09-22-09"
	//TODO:  Bug with persistent types of "Scheduled" not being displayed properly

	//echo $output;
	//  Need to match on more then just vlan 3 because port 3 would also match regex
	preg_match_all("/(.*)\s..:..:..:..:..:..\s(.*)\.(.*)\.(.*)\.(.*)/", $output, $device_matches);

	//print_r($device_matches);
	echo "<table cellpadding='0' width=\"100%\">";
	echo "<tr><th>Vlan</th><th>IP Address</th><th>Lease Expiration</th><th>Persistent</th><th>State</th><th>MAC Address</th><th>Remove</th></tr>";

	foreach ($device_matches[0] as $val) {
		//echo $val . "<br>\n";
		// Replace spaces in returned array with commas
		$val = preg_replace("/\s\s+/", ",", $val);
		// Seperate each field in the returned array into variables seperated by commas above
		list( $vlan, $mac_address, $ip_address, $state, $persistent, $weekday, $month, $date, $time, $year) = explode(",", $val);

		$lease = $weekday . " " . $month . " " . $date . " " . $time . " " . $year;
			
		//  Create association removal form here
		//  Put MAC Address next to delete button for better clarification when deleting an association
		if ($vlan != '3') {
			$bg_color = "red";
		}
		echo "<tr style=\"background: $bg_color\">";
		echo "<td>$vlan</td><td>$ip_address</td><td>$lease</td><td>$persistent</td><td>$state</td><td>$mac_address</td><td>
			<form id=\"delete_dhcp\">
				<input type=\"button\" value=\"delete\" onClick=\"delete_dhcp('$mac_address')\"/>
			</form>
			</td>";
		echo "</tr>";
		//  Send deletion command, and show reimaining associations (run a new show_dhcp_clients)
		//  New show_dhcp_clients is done in switch/case above
	}
	echo "</table>";

	//  Deletion function

	echo "<script type=\"text/javascript\">
	function delete_dhcp(mac) {
		MochaUI.notification('Deleting '+mac);
                var url='pages/blade_info.php';
		url=url+'?ip=$ip';
		url=url+'&port=$port';
		url=url+'&mac='+mac;
		url=url+'&cmd=delete_dhcp_clients';
		url=url+'&nocache='+Math.random();
		MochaUI.updateContent({
                        'element': $('panel1'),
                        'loadMethod': 'xhr',
                        'url': url,
                        //'url': 'pages/faultySTB.php',
                        'title': 'Results',
                        'padding': { top: 8, right: 8, bottom: 8, left: 8 }
                });
	};
	</script>";
}
else if ($cmd == "shut_no_shut") {
	//TODO:  Put a timer that counts down before diagnose is run again to see if shutnoshutfixed anything
	//TODO:  Wait for timer to countdown and then display a message asking CSR to rerun the diagnose on the port in question
	echo "Please wait a minute and then rerun the diagnostic on $ip port $port to see if the problem cleared up.<br/>";
	echo $output;	
}
else if ($cmd == "modem_status" OR $cmd == "ont_status") {
	//---- Get the customers information and display it ----
	preg_match("/info subscriber(.*)/", $output, $subscriber_matches);
	preg_match("/info circuit-id(.*)/", $output, $circuit_matches);
	preg_match("/info login(.*)/", $output, $customerLogin_matches);
	preg_match("/access profile(.*)/", $output, $accessProfile_matches);

	$subscriber_data = $subscriber_matches[1];
	$circuit_data = $circuit_matches[1];
	$customerLogin_data = $customerLogin_matches[1];
	if ($cmd == "ont_status") {
		$internet_speed = $accessProfile_matches[1];
	}
	else if ($cmd == "modem_status") {
		$internet_speed = $accessProfile_matches[1];
	}
	echo "Circuit: " . htmlentities($circuit_data) . "<br/>";
	echo "Subscriber: " . htmlentities($subscriber_data) . "<br/>";
	echo "Login Info: " . htmlentities($customerLogin_data) . "<br/>";
	echo "Internet: " . htmlentities($internet_speed) . "<br/>";
	
	//---- Get the modem status and display it ----
	//$modem_status = preg_replace("/STRING: /", "", $modem_status);
	echo "<table style=\"width: 100%\">";
	echo "<tr><td style=\"width: 100px\">Admin: </td>";
	if ($modem_admin_status == "down" ) {
		echo "<td style=\"text-align: center; background: #FF5555\">" . $modem_admin_status . "</td>\n";
	} 
	else {
		echo "<td style=\"text-align: center; background: #57FF57\">" . $modem_admin_status . "</td>\n";
	}
	echo "</tr>\n";
	echo "<tr><td>Operational:</td>";
	if ($modem_operation_status == "down") {
		echo "<td style=\"text-align: center; background: #FF5555\">" . $modem_operation_status . "</td>\n";
	}
	else {
		echo "<td style=\"text-align: center; background: #57FF57\">" . $modem_operation_status . "</td>\n";
	}
	echo "</tr></table>";

}
else if ($cmd == "port_performance" OR $cmd == "fiber_port_stats") {
	echo $output;
}
else if ($cmd == "occam_copper_add_customer" OR $cmd == "occam_fiber_add_customer") {
	$subName = $_GET['subName'];
	$subPhone = $_GET['subPhone'];
	$subCablePair = $_GET['subCablePair'];
	
	//  Add the changes to the port_info table
	mysql_query("UPDATE port_info SET info_1 = 'circuit-id $subPhone pair $subCablePair', info_2 = 'subscriber $subName' WHERE blade_ip = '$ip' AND port_num = '$port'"); 
	
	echo "Customer added successfully!";
	echo $output;
}
else if ($cmd == "eqn_lookup") {
	preg_match("/endpoint: \t phone\/(.*)/", $output, $endpoint);

	$endpoint_data = $endpoint[1];
	list($port,$blade) = explode("@", $endpoint_data);
	echo $blade . "/" . $port;
}
// END of output parsing section

// If debug is checked, then display the verbose output from the blade.
if ( $debug == "True" ) {
	echo $output;
}

// Close session to blade cleanly
$coblade->closetelnetsession();

?>
